// pages/allServices/allServices.js
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    // sidebar: ['热门服务', '房屋装修', '房屋维修', '工匠服务', '家政服务', '家电维修', '垃圾处理', '二手交易 '], //分类显示所需的数据	
    activeKey: 0, //左侧选中项的索引	
    sidebarName: '', //索引单个名字
    scrollTop: 0, //距离顶部
    select_index: 'select_index0',
    rightSidebar: [], //右侧高度数组
    topArr: [],
    allNumArr: [],



    // 服务类型  左侧
    queryParentList: [],
    //服务类型 子类型  右侧
    serviceList: [],
    value:'',
  },

  // 搜索
  searchOnChange(e) {
    this.setData({
      value:'',
    });
  },
  // bindSelect  选择索引触发
  bindSelect(event) {
    console.log(event);
    this.setData({
      sidebarName: this.data.queryParentList[+event.detail].typeName,
      activeKey: event.detail,
      select_index: 'select_index' + event.currentTarget.dataset.index
      // scrollTop: 0
    })

    this.queryChildrenType(this.data.queryParentList[event.currentTarget.dataset.index])

  },
  //  右侧滚动触发
  bindscrollTop(event) {
    // // console.log(event);
    // // var that = this;
    // // console.log(event.detail.scrollTop);
    // var scrollTop = event.detail.scrollTop
    // this.setData({
    //   // scrollTop:scrollTop
    // })
    // for (let i = 0; i < this.data.topArr.length; i++) {
    //   const element = this.data.topArr[i];
    //   if (scrollTop <= (element.top + element.height - 50)) {
    //     this.setData({
    //       activeKey: i
    //     })
    //     break;
    //   }
    // }
  },



  onTabCLick(e) {
    const index = e.detail.index
    this.setData({
      activeTab: index
    })
  },

  onChange(e) {
    const index = e.detail.index
    this.setData({
      activeTab: index
    })
  },
  // 获取服务类型  http://localhost:8082/manage/mobileUser/apply/queryParentList
  queryParentList() {
    var postObj = {
      url: '/mobileUser/apply/queryParentList',
      method: 'get'
    }
    app.request.loginRequest(postObj).then(res => {
      if (res.data.code === 200) {
        res.data.data.forEach((items, i) => {
          items.typeUrl = app.util.config().api + items.typeUrl
        })
        this.setData({
          queryParentList: res.data.data
        })
        console.log('获取服务类型', this.data.queryParentList);
        this.queryChildrenType(this.data.queryParentList[0])
      }
    })
  },
  //获取该类型下全部服务  /mobileUser/type/queryChildrenType 
  queryChildrenType(data) {
    var data = {
      typeId: data.typeId
    }
    var postObj = {
      url: '/mobileUser/type/queryChildrenType',
      method: 'post',
      data: data
    }
    app.request.loginRequest(postObj).then(res => {
      if (res.data.code === 200) {

        res.data.data.forEach((items, i) => {
          items.typeUrl = app.util.config().api + items.typeUrl
        })

        console.log('获取该类型下全部服务', res.data.data);
        this.setData({
          serviceList: res.data.data
        })
      }
    })
  },
  //跳转到详情服务上面去
  toService(e) {

    var oneitems = e.currentTarget.dataset.items //右侧点击的单个服务
    var activeKey = +this.data.activeKey
    var leftItems = this.data.queryParentList[activeKey] //左侧父类服务
    var index = +e.currentTarget.dataset.index //激活右侧的下标
    // console.log(leftItems);
    wx.navigateTo({
      url: `/pages/worker/workerServices/workerServices?typeName=${leftItems.typeName}&fatherTypeId=${leftItems.typeId}&sonTypeId=${oneitems.typeId}&index=${index}`,
    })
  },
  // 聚焦跳转  bind:focus='focusSearch'
  focusSearch() {
    wx.navigateTo({
      url: '/pages/search/search',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    // 获取服务类型
    that.queryParentList()

    // var typeId = options.typeId
    console.log(options);
 

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //索引单个名字
    let that = this




    // 获取右侧数组
    wx.createSelectorQuery().selectAll('.rightSidebar').boundingClientRect(function (rightSidebar) {
      console.log(rightSidebar)
      var topArr = []
      var allNumArr = []
      rightSidebar.forEach((items, i) => {
        topArr.push({
          index: items.dataset.index,
          top: items.top,
          height: items.height
        })
        allNumArr.push(items.top)
      })
      that.setData({
        topArr: topArr,
        allNumArr: allNumArr
      })
    }).exec()


    var event = wx.getStorageSync("event")
    if (!!event) {
      if (that.data.queryParentList.length>0) {
        that.bindSelect(JSON.parse(event))
      }
      
    }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})